package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdDictionary;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdDictionaryService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/9/20.
 */
@RestController
@RequestMapping("/dictionary")
public class AdDictionaryController extends  BaseController{
    private static final Logger logger = LoggerFactory.getLogger(AdDictionaryController.class);

    @Reference
    private AdDictionaryService adDictionaryService;

    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET},produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse search(HttpServletRequest request){
        String criteria = request.getParameter("criteria");
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        PageInfo pageInfo = adDictionaryService.selectAdDictionaryPaged(criteria, pageIndex, pageSize);
        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();
        map.put("items", NameCastTools.listToListMap(pageInfo.getList()));
        map.put("count", pageInfo.getTotal());
        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        String dictionaryCode = request.getParameter("dictionaryCode");
        String dictionaryName = request.getParameter("dictionaryName");
        String dictionaryValue = request.getParameter("dictionaryValue");
        String dictionaryType = request.getParameter("dictionaryType");
        String dictionaryDesc = request.getParameter("dictionaryDesc");

        AdDictionary adDictionary = new AdDictionary();
        adDictionary.setDictionaryCode(dictionaryCode);
        adDictionary.setDictionaryName(dictionaryName);
        adDictionary.setDictionaryValue(dictionaryValue);
        adDictionary.setDictionaryType(dictionaryType);
        adDictionary.setDictionaryDesc(dictionaryDesc);

        adDictionary.setDictionaryId(adDictionaryService.insert(adDictionary));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adDictionary));
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        String dictionaryIds = request.getParameter("dictionaryIds");
        JSONArray jsonArray = (JSONArray) JSON.parse(dictionaryIds);
        ArrayList<Integer> ids = new ArrayList<>();
        if (jsonArray != null) {
            int len = jsonArray.size();
            for (int i = 0; i < len; i++) {
                ids.add(jsonArray.getInteger(i));
            }
        }
        adDictionaryService.deleteByPrimaryKeys(ids);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update (HttpServletRequest request ){
        Integer dictionaryId = Integer.parseInt(request.getParameter("dictionaryId"));
        String dictionaryCode = request.getParameter("dictionaryCode");
        String dictionaryName = request.getParameter("dictionaryName");
        String dictionaryValue = request.getParameter("dictionaryValue");
        String dictionaryType = request.getParameter("dictionaryType");
        String dictionaryDesc = request.getParameter("dictionaryDesc");

        AdDictionary adDictionary= new AdDictionary();
        adDictionary.setDictionaryId(dictionaryId);
        adDictionary.setDictionaryType(dictionaryType);
        adDictionary.setDictionaryValue(dictionaryValue);
        adDictionary.setDictionaryDesc(dictionaryDesc);
        adDictionary.setDictionaryCode(dictionaryCode);
        adDictionary.setDictionaryName(dictionaryName);

        adDictionaryService.updateByPrimaryKeySelective(adDictionary);
        adDictionary =  adDictionaryService.selectByPrimaryKey(dictionaryId);

        JsonResponse  jsonResponse= new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adDictionary));
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }
    @RequestMapping(value = "/getByType", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse getByType(HttpServletRequest request) {
        String type = request.getParameter("type");
        List<AdDictionary> adDictionaryTypesList = adDictionaryService.selectadDictionaryTypes(type);
        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(adDictionaryTypesList));
        map.put("count", adDictionaryTypesList.size());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/getByCode", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse getByCode(HttpServletRequest request) {
        String code = request.getParameter("code");
        AdDictionary adDictionary= adDictionaryService.selectAdDictionaryByCode(code);
        JsonResponse jsonResponse = new JsonResponse();

        jsonResponse.setData(adDictionary);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

}
